package org.softee.functional.helper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Splitter<F> {
    public final List<F> left;
    public final List<F> right;

    /**
     * @param inputs a collection of inputs to split. It is considered an error to attempt to split a collection of
     * fewer than two elements.
     */
    public Splitter(List<F> left, List<F> right) {
        this.left = left;
        this.right = right;
    }

    public static <F> Splitter<F> split(Collection<F> inputs) {
        int size = inputs.size();
        Preconditions.assertArgument(size >= 2);
        List<F> inputList = (inputs instanceof List) ? (List<F>)inputs : new ArrayList<F>(inputs);
        int index = size / 2;
        return new Splitter<F>(inputList.subList(0, index), inputList.subList(index, size));
    }

    public static <F> List<F> merge(Collection<F> left, Collection<F> right) {
        //TODO improve performance by merging by creating a view into left and right
        List<F> result = new ArrayList<F>(left.size() + right.size());
        result.addAll(left);
        result.addAll(right);
        return result;
    }
}
